package com.webproject.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.webproject.model.bean.ConsentBean;
import com.webproject.model.interfaces.Clinic;

// TODO: Auto-generated Javadoc
/**
 * JavaBean Form controller that is used to edit an existing
 * <code>Patient</code>.
 * 
 * @author Juergen Hoeller
 * @author Ken Krebs
 * @author Arjen Poutsma
 */
@Controller
@RequestMapping("/consent/{patientId}/consent")
@SessionAttributes(types = ConsentBean.class)
public class AddConsentController {

	/** The clinic. */
	private final Clinic clinic;

	/**
	 * Instantiates a new adds the consent controller.
	 * 
	 * @param clinic
	 *            the clinic
	 */
	@Autowired
	public AddConsentController(Clinic clinic) {
		this.clinic = clinic;
	}

	/**
	 * Load consent.
	 * 
	 * @param patientId
	 *            the patient id
	 * @param model
	 *            the model
	 * @return the string
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String loadConsent(@PathVariable("patientId") int patientId,
			Model model) {
		ConsentBean consent = this.clinic.loadConsent(patientId);
		model.addAttribute(consent);
		return "consent/consent";
	}

	/**
	 * Process submit.
	 * 
	 * @param consent
	 *            the consent
	 * @param result
	 *            the result
	 * @param status
	 *            the status
	 * @return the string
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(@ModelAttribute ConsentBean consent,
			BindingResult result, SessionStatus status) {
		return "redirect:/patients/search";
	}

	/**
	 * Sets the consent fields.
	 * 
	 * @param dataBinder
	 *            the new consent fields
	 */
	@InitBinder
	public void setConsentFields(WebDataBinder dataBinder) {
		dataBinder.setDisallowedFields("id");
	}

}
